# Jay Jang 4/1/2021
# R version 4.0.3 (2020-10-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)

library(tidyverse)
library(lfe)
library(stargazer)

setwd('C:/Users/battl/OneDrive/JUN/UC MERCED/Independent Research/local election and media freedom')

df <- read_csv('data/clean/df.csv') 

#==============#
# FE: survival #
#==============#

# lag_ellocelc_bi

fe_loc_base <- felm(noncivilfail ~ lag_ellocelc_bi * lag_msfs + 
                    poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                    lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                    data=df)

# lag_elsrgel_bi

fe_reg_base <- felm(noncivilfail ~ lag_elsrgel_bi * lag_msfs + 
                      poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                      lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                    data=df)


# lag_xellocelec

fe_locq_base <- felm(noncivilfail ~ lag_xellocelec * lag_msfs + 
                       poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                       lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                     data=df)

# lag_xelregelec

fe_regq_base <- felm(noncivilfail ~ lag_xelregelec * lag_msfs + 
                       poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                       lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                     data=df)

## report: Table 1
stargazer(fe_loc_base, fe_reg_base, fe_locq_base, fe_regq_base, omit.stat=c("rsq", "adj.rsq", "ser"),
          omit = c("lag_gdp", "lag_growth", "lag_logoil", "lag_war", "lag_interwar", 
                   "lag_natelec", "lag_personalism", "gwf_case_duration"),
          covariate.labels=c("Local Election","Regional Election","Local Quality", "Regional Quality",
                             "Media Freedom", "Local Election * Media Freedom", "Regional Election * Media Freedom",
                             "Local Quality * Media Freedom", "Regional Quality * Media Freedom"),
          dep.var.labels.include = FALSE, model.numbers = FALSE,
          df=F, column.labels = c("Local","Regional","Loc Quality", "Reg Quality"), 
          dep.var.caption  = "DV: Regime Breakdown",
          add.lines = list(c("Regime-Fixed", "Y", "Y","Y", "Y"),
                           c("Year-Fixed", "Y", "Y","Y", "Y"),
                           c("Confounders", "Y", "Y","Y", "Y"),
                           c("Poly(Duration,3)", "Y", "Y","Y", "Y")),
          notes = "SE clustered at the regime in parentheses.",
          type = 'html', out='main.doc')


#==============#
# Placebo Test #
#==============#

# national election * media freedom
natmed <- felm(noncivilfail ~ lag_natelec * lag_msfs + 
                      poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                      lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                    data=df)


# V-Dem Liberal democracy as moderator 

liblocbi <- felm(noncivilfail ~ lag_ellocelc_bi * lag_libdem + 
                      poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                      lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                    data=df)

libregbi <- felm(noncivilfail ~ lag_elsrgel_bi * lag_libdem + 
                      poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                      lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                    data=df)

libloq <- felm(noncivilfail ~ lag_xellocelec * lag_libdem + 
                       poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                       lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                     data=df)

libreq <- felm(noncivilfail ~ lag_xelregelec * lag_libdem + 
                       poly(gwf_case_duration,3)+ lag_gdp + lag_growth+
                       lag_logoil + lag_war + lag_interwar+lag_natelec + lag_personalism| caseid + year | 0 | caseid,
                     data=df)

## report: Table 2
stargazer(natmed, liblocbi, libregbi, libloq, libreq, omit.stat=c("rsq", "adj.rsq", "ser"),
          omit = c("lag_gdp", "lag_growth", "lag_logoil", "lag_war", "lag_interwar", 
                   "lag_personalism", "gwf_case_duration"),
          #covariate.labels=c("Local Election","Regional Election","Local Quality", "Regional Quality",
          #                   "Liberalization", "Local Election * Liberalization", "Regional Election * Liberalization",
          #                   "Local Quality * Liberalization", "Regional Quality * Liberalization"),
          dep.var.labels.include = FALSE, model.numbers = FALSE,
          df=F, column.labels = c("NE * MF","LE * LD","RE * LD", "LQ * LD", "RQ * LD"), 
          dep.var.caption  = "DV: Regime Breakdown",
          add.lines = list(c("Regime-Fixed", "Y", "Y","Y", "Y","Y"),
                           c("Year-Fixed", "Y", "Y","Y", "Y","Y"),
                           c("Confounders", "Y", "Y","Y", "Y","Y"),
                           c("Poly(Duration,3)", "Y", "Y","Y", "Y","Y")),
          notes = "SE clustered at the regime in parentheses.",
          type = 'html', out='placebo.doc') # I manually modify this table to generate Table 2.

